package com.example.tushareapi;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
//import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class HttpRequestPost {

    public static JSONObject deviceRequest() {

        JSONObject result = null;

        try {
            String url = "http://api.waditu.com";
            JSONObject json = new JSONObject();
            // json.put("{\"api_name\": \"stock_basic\", \"token\": \"531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a\", \"params\": {\"list_stauts\":\"L\"}, \"fields\": \"ts_code,name,area,industry,list_date\"}" );
            json.put("api_name","stock_basic");
            json.put("token","531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a");

            JSONObject json2 = new JSONObject();
            json2.put("list_stauts","L");
            json.put("params", json2);
            json.put("fields","ts_code,name,area,industry,list_date");
            System.out.println(json);
            @SuppressWarnings({"resource"})
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            StringEntity postingString = new StringEntity(json.toJSONString());
            post.setEntity(postingString);
            post.setHeader("Content-type", "application/json");
            HttpResponse response = httpClient.execute(post);
            String content = EntityUtils.toString(response.getEntity());

            result = (JSONObject) JSONObject.parse(content);

            System.out.println(result);

        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }

        return result;
    }
}
